<html>
<head>

<link rel="stylesheet" href="style.css">
<link type="text/css" href="css/smoothness/jquery-ui-1.8.11.custom.css" rel="Stylesheet" />	
<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="development-bundle/external/jquery.cookie.js"></script> 
<script type="text/javascript" src="js/jquery-ui-1.8.11.custom.min.js"></script>

<script>
$(function() {
	$( "#tabs" ).tabs({
		cookie: {
			// store cookie for a day, without, it would be a session cookie
			expires: 1
		}
	});
});
</script>

<script>
	var proov = new Array(<?php require 'studentdata.php';?>);
	$(function() {
		$( "#search" ).autocomplete({
			source: proov,
			minLength: 2,
			
			select: function( event, ui ) {
				//console.log(event);
				document.getElementById('hid').value=ui.item.value;
				//alert(ui.item.value);
			} 
		});
	});
</script>

<script type="text/javascript">
jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
</script>

<script type="text/javascript">
	//function showContains(value){}
	$(function() {
    	$('#search2').keyup(function() {
        	$('tr').hide();
			$('tr:Contains("'+$(this).val()+'")').show();
    	});
    });	
</script>

</head>

<body>
	<a href="Groups.php" ><b>BACK</b></a>
    <div id="tabs">
		<ul>
			<li><a href='#tabs-1'>Existing User</a></li>
			<li><a href='#tabs-2'>Not a user</a></li>
		</ul>
    

        <div id="tabs-1">

        	<form action="AddPeople.php" method="post">
        	<div id="groupmain">
            	<div id="tab2left">
            	
            	<?php 
                	require 'dbconnect.php';
                	$result = mysql_query("SELECT * FROM students");

                	echo "<table border='1'>
                    <tr>
                        <th>Name</th>
                        <th>Isikukood</th>
                        <th>Select</th>
                    </tr>";
               
                	while($row = mysql_fetch_array($result)){
                	    echo "<tr>";
                	    echo "<td>". $row['nimi']. "</td>";
                	    echo "<td>". $row['isikukood']. "</td>";
                	    echo "<td>". "<input type=\"checkbox\" name=\""."sid[".$row['id']."]"."\"/>"."</td>";
                	    echo "</tr>";
                	}
                	echo "</table>";

                	if(isset($_POST['sid'])){
                	    $ids = array($_POST['sid']);
                	    $idskeys = array_keys($ids[0]);
                	}
                
            	?>
         		</div> <!-- tab-1 left -->
         		
         		<div id="tab2right">
             			<select name="gid">
             				<?php 
             				$result2 = mysql_query("SELECT * FROM groups");
             				while($row = mysql_fetch_array($result2)){
             				    echo "<option value=\"".$row['id']."\" >".$row['groupname']."</option>";
             				}
             				?>
             			</select>
             			<?php 
             				$gids = &$_POST['gid'];
             				if(isset($_POST['sid'])){
                        	    $ids = array($_POST['sid']);
                        	    $idskeys = array_keys($ids[0]);
     
             				  	foreach ($idskeys as $key){
                        	         $query = sprintf("INSERT INTO groupstudent (group_id, student_id)
                        	         VALUES('%s','%s');",mysql_real_escape_string($gids),
                        	         mysql_real_escape_string($key)
                        	         );
                        	         mysql_query($query);
                    	        }
             				}
             			?>
             			
             			<input type="submit" value="Submit">
             			<input id="search2" name="search2" type="text">
             			
         		</div>	
         		
         		<div class="clear">
         		
         		</div>	
     		</div> <!-- main div -->
     		</form>
     		
        </div>
        
        <div id="tabs-2">
        	<form action="AddPeople.php" method="post">
	        	<table>
	        	<thead>Not a user</thead>
		        	<tr>
		        		<td>Name</td>
		        		<td><input name="notusername" type="text" size="15"> </td>
		        	</tr>
		        	<tr>
		        		<td>Phone number</td>
		        		<td><input name="notuserphone" type="text" size="15"></td>
		        	</tr>
		        	<tr>
		        		<td>Email</td>
		        	<td><input name="notuseremail" type="text" size="15"></td>
		        	</tr>
	        	</table>
	        	<input type="submit" value="Submit">
        	</form>
        	<?php 
        		
        		require 'dbconnect.php';
        		$notusername = &$_POST['notusername'];
        		$notuserphone = &$_POST['notuserphone'];
        		$notuseremail = &$_POST['notuseremail'];
        		
        		if (isset($notusername)){
        			$query3 = sprintf("INSERT INTO notauser (name, phone, email) VALUES ('%s','%s','%s');",
        			mysql_escape_string($notusername),
        			mysql_escape_string($notuserphone),
        			mysql_escape_string($notuseremail)
        			);
        			//echo $query3;
        			$result3 = mysql_query($query3);
        			if($result3 != false){
        				echo "User added";
        			}
        		}
        	?>
        	
        </div>
    </div>
</body>
</html>

